package maiyatian.videolive.fragment.newest;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;

import org.xutils.view.annotation.ContentView;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.IFragment;
import maiyatian.videolive.MainActivity;
import maiyatian.videolive.R;
import maiyatian.videolive.adapter.newest.NewestAdapter;
import maiyatian.videolive.bean.LiveBean;
import maiyatian.videolive.widget.GridSpacingItemDecoration;
import maiyatian.videolive.widget.VerticalSwipeRefreshLayout;

/**
 * Created by 王中阳 on 2016/5/10.
 */
@ContentView(R.layout.fragment_newest)
public class NewestFragment extends IFragment implements NewestAdapter.IClickListener, SwipeRefreshLayout.OnRefreshListener {
    public static final String TAG = "NewestFragment";
    MainActivity mycontext;

    List<LiveBean> datas;
    private NewestAdapter mAdapter;
    private RecyclerView recyclerView;
    private VerticalSwipeRefreshLayout mSwipeLayout;

    public NewestFragment() {

    }

    public static NewestFragment newInstance() {
        NewestFragment fragment = new NewestFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mycontext = (MainActivity) context;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updateUI();
    }

    private void updateUI() {
        recyclerView = (RecyclerView) mycontext.findViewById(R.id.rvLiveLast);
        RecyclerViewHeader header = (RecyclerViewHeader) mycontext.findViewById(R.id.header);
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 20, true));
        header.attachTo(recyclerView, true);
        mAdapter = new NewestAdapter(mycontext, this);
        datas = new ArrayList<>();
        recyclerView.setAdapter(mAdapter);
        //刷新数据的方法
        mSwipeLayout = (VerticalSwipeRefreshLayout) mycontext.findViewById(R.id.swipeRefresh);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_blue_bright, android.R.color.holo_blue_bright,
                android.R.color.holo_blue_bright);
        //需要实现方法
        mSwipeLayout.setOnRefreshListener(this);
        updateFromNet();
    }

    private void updateFromNet() {
        for (int i = 0; i < 10; i++) {
            LiveBean liveBean = new LiveBean();
            datas.add(liveBean);
        }
        mAdapter.setMdatas(datas);
        mHandler.sendEmptyMessage(0);
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void onItemClicked(int position) {

    }

    //刷新数据相关
    public void setSwipeToRefreshEnabled(boolean enabled) {
        mSwipeLayout.setEnabled(enabled);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (mSwipeLayout.isRefreshing()) {
                        mSwipeLayout.setRefreshing(false);
                    }
                    break;
            }
        }
    };

    @Override
    public void onRefresh() {
        updateFromNet();
    }
}
